import email
from webbrowser import get
from django.shortcuts import render
from .models import *
from django.http import HttpResponse, JsonResponse
from django.core import serializers
import json

# Create your views here.

def register(request):
    '''
    用户注册

    '''
    
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        print(data)
        try:
            username = data['username']
            # user = User.objects.get(username=username)
            # if not user:
            password = data['password'] 
            birthday = data['birthday']
            gender = data['gender']   
            address = data['address']  
            phone = data['phone']
            email = data['email']

            User.objects.create(username=username, 
                password=password, 
                birthday=birthday, 
                gender=gender, 
                address=address, 
                phone=phone,
                email=email)
            
            JsonResponse({'msg': '注册成功', 'code': 0}, safe=False)
            # else:
            #     JsonResponse({'msg': '请完整填写信息', 'code': -1})
        except Exception:
            JsonResponse({'msg': '请完整填写信息'}, safe=False)

        return JsonResponse({'msg': 'need post'}, safe=False)
    return JsonResponse({'msg': 'need post'}, safe=False)

def login(request):
    '''
    用户登陆

    '''
    
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        # print(data)
        try:
            username = data['username']
            password = data['password']
            
            user = User.objects.get(username=username)
            if user.password == password:     
                return JsonResponse({"code": 1,'msg': '登陆成功', "data": str(user)}, safe=False)
            else:
                JsonResponse({"code": 0,'msg': '账号密码不正确'}, safe=False)
            
        except Exception:
            JsonResponse({"code": 0,'msg': '账号密码不正确'}, safe=False)
        return JsonResponse({"code": 0, 'msg': '账号密码不正确'}, safe=False)
    else:
        return HttpResponse('{"code": 0,"msg":"method must be POST"}')


def update(request):
    '''
    更新
    '''
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        try:
            username = data['username']
            password = data['password'] 
            birthday = data['birthday'] 
            gender = data['gender']   
            address = data['address']  
            phone = data['phone']    
            email = data['email']   
            User.objects.filter(username=username).update(
                    password=password, 
                    birthday=birthday, 
                    gender=gender, 
                    address=address, 
                    phone=phone,
                    email=email)
                
            JsonResponse({'msg': '修改成功', 'code': 0})
            
        except Exception:
            JsonResponse({'msg': '请完整填写信息'})
        return JsonResponse({'msg': 'need post'})
    return JsonResponse({'msg': 'need post'})